package com.example.weather.Adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.weather.R;

import java.util.List;

public class DeletecityAdapyer extends BaseAdapter {
    Context context;
    List<String> mdata;//城市数据源
    List<String> dcity;//删除的城市的数据源

    public DeletecityAdapyer(Context context, List<String> mdata,List<String> dcity) {
        this.context = context;
        this.mdata = mdata;
        this.dcity=dcity;
    }

    @Override
    public int getCount() {
        return mdata.size();
    }

    @Override
    public Object getItem(int position) {
        return mdata.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Viewholder holder=null;
        if (convertView==null){
            convertView= LayoutInflater.from(context).inflate(R.layout.item_deletecity,null);
            holder = new Viewholder(convertView);
            convertView.setTag(holder);
        }else {
            holder= (Viewholder) convertView.getTag();
        }
        final String city = mdata.get(position);
        holder.item_delete_tv.setText(city);
        holder.item_delete_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mdata.remove(city);
                dcity.add(city);
                notifyDataSetChanged();//删除提示更新
            }
        });
        return convertView;
    }

    class Viewholder{
        TextView item_delete_tv;
        ImageView item_delete_iv;
        public Viewholder(View view){
            item_delete_tv=view.findViewById(R.id.item_delete_tv);
            item_delete_iv=view.findViewById(R.id.item_delete_iv);
        }
    }
}
